﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmCreateLesson : Form
    {
        #region "BIẾN"
        public int userID;
        public int slbt = 0;
         //Các ký tự đặc biệt
        char[] specialChar={'+','-','.',',','/','?','"',':','\\',';','=','\'','`','~','@','#','$','%','^','&','*','(',')','-','|'};
        //Biến đếm số đoạn văn
        int paragraphCount = 0;
        //List danh sách đoạn văn
        List<string> ParagraphList = new List<string>();
        #endregion
        #region "Hàm kiểm tra nhập liệu"
        //Hàm kiểm tra Tên bài học
        bool CheckLessonName(string lsName)
        {
           
            lsName=lsName.Trim();
            if (lsName == "")
                return false;
            for (int i = 0; i < lsName.Length; i++)
                for (int j = 0; j < specialChar.Length; j++)
                    if (lsName[i].Equals(specialChar[j]))
                        return false;
            return true;

        }
        //Hàm kiểm tra chuỗi số
        bool isNumber(string num)
        {
            if (num.Length < 1)
                return false;
            for (int i = 0; i < num.Length; i++)
                if (!char.IsNumber(num[i]))
                    return false;
            return true;
        }
        //Hàm kiểm tra nội dung của đoạn văn bản
        bool CheckParagraph(string paragraph)
        {
            paragraph = paragraph.Trim();
            if (paragraph == "")
                return false;
            return true;
        }
        //Hàm set avatar, level
        void LoadAvatar_Level()
        {
            string un = CLASS.CNguoiDung.SetAvatar_Level(userID, pbAvatar, pbLevel);
            lblUsername.Text = un;
            ToolTip toolTip1 = new ToolTip();
            toolTip1.SetToolTip(pbLevel, pbLevel.Tag.ToString());
        }
        #endregion
        public frmCreateLesson()
        {
            InitializeComponent();
        }

        private void frmCreateLesson_Load(object sender, EventArgs e)
        {
            LoadAvatar_Level();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (slbt == 10)
            {
                MessageBox.Show("Sorry,List is full", "Noices");
                return;
            }
            if (!CheckLessonName(txtLessonName.Text))
            {
                MessageBox.Show("Lesson Name is not valid");
                txtLessonName.Focus();
                return;
            }
            if (!isNumber(txtTime.Text))
            {
                MessageBox.Show("Time must be an integer");
                txtTime.Focus();
                return;
            }
            if (ParagraphList.Count < 4)
            {
                MessageBox.Show("Paragraph count must great than 4");
                return;
            }
            CLASS.CBaiTap lesson = new Typing_GUI.CLASS.CBaiTap();
            lesson.Tenbaitap = CLASS.CBaiTap.RemoveQuote(txtLessonName.Text);
            lesson.Diemtoida = 1;
            lesson.Thoigian = int.Parse(txtTime.Text);
            lesson.Mota = CLASS.CBaiTap.RemoveQuote(txtDescription.Text);
            lesson.Loaibaitap = 1;

            lesson.Nguoisoan = userID;

            

            lesson.TinhTrang = false;
            lesson.Capdobaitap = 1;
            string strParagraphs = "";
            for (int i = 0; i < ParagraphList.Count; i++)
            {
                strParagraphs += ParagraphList[i];
                if (i < ParagraphList.Count - 1)
                {
                    strParagraphs += "[P]";
                }
            }
            lesson.Noidung = CLASS.CBaiTap.RemoveQuote( strParagraphs);
            if (lesson.CreateLesson())
            {
                MessageBox.Show("Lesson have created successful!");
                lbParagraphs.Items.Clear();
                ParagraphList.Clear();
                slbt++;
            } 
            else
                MessageBox.Show("Lesson have created failed!"); 
            
        }

        private void btnClearField_Click(object sender, EventArgs e)
        {
            txtLessonName.Text = "";
            txtMaxScore.Text = "0";
            txtDescription.Text = "";
            txtParagraph.Text = "";
        }

        private void btnClearParagraph_Click(object sender, EventArgs e)
        {
            txtParagraph.Text = "";
        }

        private void btnAddParagraph_Click(object sender, EventArgs e)
        {
            if (CheckParagraph(txtParagraph.Text))
            {
                string str = "Paragraph ";
                paragraphCount++;
                lbParagraphs.Items.Add(str + paragraphCount.ToString());
                ParagraphList.Add(txtParagraph.Text);
                txtParagraph.Text = "";
                lblParagraphCount.Text = paragraphCount.ToString();
            }
            else
            {
                MessageBox.Show("Paragraph is not valid");
                txtParagraph.Focus();
                return;
            }
        }

        private void lbParagraphs_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index=lbParagraphs.SelectedIndex;
            if (index!=-1)
            {
                txtParagraph.Text = ParagraphList[index];
                btnDelete.Enabled = true;
                btnUpdate.Enabled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int index = lbParagraphs.SelectedIndex;
            if (index != -1)
            {
                ParagraphList.RemoveAt(index);
                lbParagraphs.Items.RemoveAt(index);
                txtParagraph.Text = "";
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckParagraph(txtParagraph.Text))
            {
                MessageBox.Show("Paragraph is not valid");
                txtParagraph.Focus();
                return;
            }
            else
            {
                int index = lbParagraphs.SelectedIndex;
                if (txtParagraph.Text.Length > 0)
                {
                    ParagraphList[index] = txtParagraph.Text;
                }
            }
        }

        private void btnBackToMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}